<?php

namespace app\admin\controller\datacenter;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use think\Config;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Annalroom extends Backend
{
    
    /**
     * YOnlineorder模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('VDayRoom');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //按时间搜索
        $startime = $this->request->param('startime');
        $endtime = $this->request->param('endtime');
        if(empty($startime) && empty($endtime)){
            $startime = strtotime('-30 days');
            $endtime = time();
        }elseif(empty($startime) && !empty($endtime)){
            $endtime = strtotime($endtime);
            $startime = $endtime-(86400*30);
        }elseif(!empty($startime) && empty($endtime)){
            $startime = strtotime($startime);
            $endtime = $startime+(86400*30);
        }else{
            $startime = strtotime($startime);
            $endtime = strtotime($endtime);
            if(($endtime - $startime) >(86400*30)){
               $this->error('最多查30天记录'); 
            }
        }
        
        $map['createtime'] = array('between', [$startime,$endtime]);
        
        //搜索30条
        $list = $this->model->where($map)->order('createtime asc')->select();

        $game_list = Config::get('GameName');
        unset($game_list[0]);
        
        $tnum = $column = $roomcode = [];
        foreach ($game_list as $kg=>$vg){
            $tnum[$kg] = [];
        }
        
        foreach ($list as $key=>$val){
            
            $a = json_decode($val['gameroom'],true);//转数据
            $b = []; 
            foreach ($a as $k=>$v){
                $b[$v['kinId']] = $v['num'];
            }
            foreach ($tnum as $kg=>$vg){
                $tnum[$kg][] = isset($b[$kg])?$b[$kg]:0;
            }
            
            $column[] = date('m.d',$val['createtime']);//时间
            $roomcode[] = $val['diamond'];//房卡
        }
        
        $this->view->assign([
            'tnum'  => $tnum,//配置中有的游戏数据 array(100=>array(7,10,11),210=>array(12,32,33))
            'roomcode' => $roomcode,//房卡
            'column' => $column,//时间
            'gameid' => $game_list,//游戏id=>游戏名称
            'gamecolor' => Config::get('GameColor'),//游戏id=>显示颜色
            'gamelist' => array_merge($game_list,['房卡消耗']),//颜色说明
            'is_mobile' => is_mobile()?:0,//是否手机查看
            'endtime' => date('Y-m-d',$endtime),//开始时间
            'startime' => date('Y-m-d',$startime),//结束时间
        ]);
        
        return $this->view->fetch();
    }
    
    /**
     * 每天统计列表
     */
    public function lists(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('pkey_name'))
        {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        
        $total = $this->model
        ->where($where)
        ->order($sort, $order)
        ->count();
    
        $list = $this->model
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();
        
        foreach ($list as $key=>$val){
            $a = json_decode($val['gameroom'],true);//转数据
            $b = [];
            foreach ($a as $k=>$v){
                $list[$key]['game'.$v['kinId']] = $v['num'];
            }
            $list[$key]['createtime'] = date('Y-m-d',$val['createtime']);
        }
        
        //统计
        $timestamp = time();
        $w_star = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)));
        $w_end = strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1;
        $statistics = [];
        $c_num = $this->model->field('sum(roomnum) as roomnum,sum(diamond) as diamond')->find();//总
        $m_num = $this->model->field('sum(roomnum) as roomnum,sum(diamond) as diamond')->where("FROM_UNIXTIME(createtime,'%Y-%m') = date_format(now(),'%Y-%m')")->find();//月
        $w_num = $this->model->field('sum(roomnum) as roomnum,sum(diamond) as diamond')->where(['createtime'=>array('between', [$w_star,$w_end])])->find();//周
        $diamond = Db::name('v_annal_room')->where("FROM_UNIXTIME(endtime/1000,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')")->sum('roomcode'); //今日房卡消耗(大厅+俱乐部)
        $roomnum = Db::name('v_annal_room')->field('roomid')->where("roomcode > 0 and FROM_UNIXTIME(endtime/1000,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')")->group('roomid,createtime')->count();//今日开房数(大厅+俱乐部)
            
        $statistics['c_roomnum'] = empty($c_num['roomnum'])?0:$c_num['roomnum'];//开房数
        $statistics['c_diamond'] = empty($c_num['diamond'])?0:$c_num['diamond'];//房卡数
        $statistics['m_roomnum'] = empty($m_num['roomnum'])?0:$m_num['roomnum'];
        $statistics['m_diamond'] = empty($m_num['diamond'])?0:$m_num['diamond'];
        $statistics['w_roomnum'] = empty($w_num['roomnum'])?0:$w_num['roomnum'];
        $statistics['w_diamond'] = empty($w_num['diamond'])?0:$w_num['diamond'];
        $statistics['d_roomnum'] = $roomnum;
        $statistics['d_diamond'] = $diamond;
        
        $result = array("total" => $total, "rows" => $list, "statistics"=>$statistics);
    
        return json($result);
    }

}
